<template>
  <el-popover ref="popoverRef" trigger="click" placement="bottom-start" popper-class="p0" :width="240" @show="showList" @hide="hideList">
    <template #reference>
      <div class="search-item" :class="{ active: visible, isSelect: !!selectText }" @click="showList">
        <span class="label">{{ formItem.props?.label }}<span v-if="selectText">:</span></span>
        <span class="text">{{ selectText }}</span>
        <el-icon class="array-icon" :size="10" color="#999">
          <svg-icon name="ep:search" />
        </el-icon>
        <el-icon class="delete-icon" :size="10" color="#999" @click.stop="clear">
          <svg-icon name="ep:circle-close-filled" />
        </el-icon>
      </div>
    </template>

    <div class="select-list-wrap">
      <div class="search-wrap">
        <input v-model="keyword" type="text" :placeholder="attr.placeholder" @input="searchTeacher" @keydown.enter="search">
        <el-icon v-show="keyword !== ''" color="#333" :size="14" @click="search">
          <svg-icon name="ep:search" />
        </el-icon>
      </div>
      <div v-loading="loading" class="select-list" style="min-height: 100px;">
        <div v-for="item in options" :key="item.value" :class="{ active: attr.multiple && Array.isArray(selected) ? selected.includes(item.id) : selected === item.id }" class="select-item" @click="selectItem(item)">
          <span class="item-text w100" :title="item.name">{{ item.name }}</span>
          <!--          <el-icon :size="12" color="#247FFC">-->
          <!--            <svg-icon name="ep:check" />-->
          <!--          </el-icon>-->
        </div>

        <div v-if="options.length <= 0" class="txt-center font12 color-sub pt30">没有查询到老师~</div>
      </div>

      <div v-if="attr.multiple" class="select-footer">
        <el-button :auto-insert-space="false" type="primary" size="small" style="height: 28px;" @click="sureSelect">确定</el-button>
      </div>
    </div>
  </el-popover>
</template>

<script setup>
import { watch } from 'vue'
import { useDebounceFn } from '@vueuse/core'
import { getTeacherByName } from 'api/common'

const props = defineProps({
  fields: [String, Number, Array],
  attr: {
    type: Object,
    default: () => {
      return { placeholder: '请选择' }
    }
  },
  formItem: {
    type: Object
  }
})
const emit = defineEmits(['update:fields', 'search'])
const visible = ref(false)
const loading = ref(false)
const popoverRef = ref(null)
const selected = ref(props.attr.multiple ? [] : null)
const keyword = ref('')
const options = ref([])

watch(() => props.fields, () => {
  selected.value = props.fields
  if (!props.fields) {
    keyword.value = ''
    selectText.value = ''
  }
})

function searchTeacher(e) {
  queryList(e.target.value)
}

const queryList = useDebounceFn(query => {
  if (query) {
    loading.value = true
    getTeacherByName({ name: query }).then(res => {
      options.value = res.result
    }).finally(() => {
      loading.value = false
    })
  } else {
    options.value = []
  }
}, 500)

const selectText = ref('')

function showList() {
  visible.value = true
}

function hideList() {
  visible.value = false
}

function selectItem(data) {
  if (props.attr.multiple) {
    if (!Array.isArray(selected.id)) {
      selected.value = []
    }
    selected.value = selected.value.includes(data.id) ? selected.value.filter(item => item !== data.id) : [...selected.value, data.id]
  } else {
    selected.value = data.name.split('(')[0]
    selectText.value = data.name
    sureSelect()
  }
}

function sureSelect() {
  emit('update:fields', selected.value)
  emit('search')
  visible.value = false
  popoverRef.value.hide()
}

function search() {
  selected.value = keyword.value
  emit('update:fields', keyword.value)
  emit('search')
  visible.value = false
  popoverRef.value.hide()
}

function clear() {
  selected.value = props.attr.multiple ? [] : null
  keyword.value = ''
  selectText.value = ''
  emit('update:fields', selected.value)
  emit('search')
}
</script>

<style scoped lang="scss">
@import "./search.scss";
.search-wrap {
  margin: 10px 10px 0;
  border-bottom: 1px solid #e5e5e5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  input {
    box-shadow: none;
    border: none;
    outline: none;
    flex: 1;
    padding: 5px;
    font-size: var(--font-size-min);
  }
}
</style>
